package com.thb.business.dao.Impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.thb.business.dao.IOrderDao;
import com.thb.business.model.Order;

@Repository
public class IOrderDaoImpl implements IOrderDao {

	@Autowired
	private JdbcTemplate jTemplate;

	@Override
	public List<Order> findByPara(Order para) {
		StringBuilder sql = new StringBuilder("");
		sql.append("select a.* from t_order a where a.uid = ? limit ?,?");
		Object[] arg = new Object[]{para.getUid(),(para.getPage()-1)*para.getPageSize(),para.getPageSize()};
		
		return jTemplate.query(sql.toString(), arg,new RowMapper<Order>() {
			
			public Order mapRow(ResultSet rs, int no) throws SQLException {

				return jsonMapper(rs, no);
			}
		});
	}

	private Order jsonMapper(ResultSet rs, int no) throws SQLException {

		Order vo = new Order();
		vo.setId(rs.getInt("id"));
		vo.setCreateTime(rs.getString("create_time"));
		vo.setDesc(rs.getString("desc"));
		vo.setTotal(rs.getInt("total"));
		vo.setType(rs.getInt("type"));
		
		return vo;
	}

	@Override
	public int countByPara(Order para) {
		StringBuilder sql = new StringBuilder("");
		sql.append("select a.* from t_order a where a.uid = ? ");
		Object[] arg = new Object[]{para.getUid()};
		return jTemplate.queryForInt(sql.toString(), arg);
	}
}
